package com.erp.activity

import android.app.Activity
import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.AdapterView
import androidx.appcompat.app.AlertDialog
import com.erp.Interface.OnUserDataInterface
import com.erp.R
import com.erp.bean.WtPhotoBean
import com.erp.mclass.QuanXian
import com.erp.myAdapter.WenTiTKAdapter
import com.erp.util.Httputil
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.activity_danju_wt.*
import kotlinx.android.synthetic.main.header_knoya1.*
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject

class DanjuWTKActivity : BaseActivity(), AdapterView.OnItemLongClickListener, AdapterView.OnItemClickListener, View.OnClickListener {
    lateinit var adapter: WenTiTKAdapter
    private var oid =""
    private var shop_name = ""
    private var contract_num = ""
    private var onum = ""
    lateinit var wtphotobeans: MutableList<WtPhotoBean>
    lateinit var updatedialog: AlertDialog
    var code = 0
    var isXuan = false
    private val a = "DanjuWTActivity"
    override fun getLayoutId(): Int {
        activityname = a
        return R.layout.activity_danju_wt
    }
    override fun initCreate() {
        iv_header_left.visibility = View.VISIBLE
        tv_header_right.visibility = View.VISIBLE
        tv_header_right.text = "拍照"
        oid = intent.getStringExtra("oid")
        code = intent.getIntExtra("code", code)
        wtphotobeans = ArrayList<WtPhotoBean>()
        adapter = WenTiTKAdapter(this, wtphotobeans,code)
        tv_header_title.text = "质量问题照片"
        gv_photo.adapter = adapter
        gv_photo.onItemLongClickListener = this
        getHttpPhoto()
        gv_photo.onItemClickListener = this
        Log.d("onClick","code="+code)
        when (code) {
            1 -> {
                tv_header_title.text = "质量问题图片"
                onum = intent.getStringExtra("onum")
                shop_name = intent.getStringExtra("shop_name")
                contract_num = intent.getStringExtra("contract_num")
                tvdizhi.text=shop_name+"   合同号："+contract_num+"   生产单号："+onum
            }
            2 -> {
                tv_header_title.text = "订单内容图片"
                onum = intent.getStringExtra("onum")
                shop_name = intent.getStringExtra("shop_name")
                contract_num = intent.getStringExtra("contract_num")
                tvdizhi.text=shop_name+"   合同号："+contract_num+"   生产单号："+onum
            }
            3 -> {
                tv_header_title.text = "安装图片详细"
            }
            4 -> {
                tv_header_title.text = "专卖店照片"
            }
        }
        iv_header_left.setOnClickListener(this)
        tv_header_right.setOnClickListener(this)
        tv_shanchu.setOnClickListener(this)
        tv_quxiao.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v) {
            iv_header_left -> finish()
            tv_header_right -> {
                if (code == 1) {
                    if (!QuanXian.iseditquality) {
                        Myutil.MyTost("你没有上传质量问题图片的权限")
                        return
                    }
                } else if (code == 2) {
                    if (!QuanXian.iseditorder) {
                        Myutil.MyTost("你没有上传订单内容图片的权限")
                        return
                    }
                } else if (code == 4) {
                    if (!QuanXian.isBianJiZhuanMaiDian) {
                        Myutil.MyTost("你没有上传订单内容图片的权限")
                        return
                    }
                }
                adapter.listcb.clear()
                adapter.notifyDataSetChanged()
                rl_herder.visibility = View.GONE
                val intent = Intent(this@DanjuWTKActivity, WtPhotoActivity::class.java)
                intent.putExtra("oid", oid)
                intent.putExtra("code", code)
                startActivityForResult(intent, 1)
            }
            tv_shanchu -> {
                val listfid = ArrayList<String>()
                for (i in adapter.listcb.indices) {
                    listfid.add(adapter.channelList[adapter.listcb[i]].fid)
                }

                updatedialog = Myutil.showupdatedialog(this@DanjuWTKActivity, "正在删除", false)
                if (code == 4) {
                    Httputil.zhuanmaidianPhoto(this@DanjuWTKActivity, oid, listfid, object : OnUserDataInterface {
                        override fun onUserData(data: String) {
                            Log.d("TAAAG", "data=" + data)
                            updatedialog.dismiss()
                            adapter.listcb.sort()
                            for (i in adapter.listcb.size-1 downTo 0){
                                wtphotobeans.removeAt(adapter.listcb[i])
                            }
                            adapter.clean()
                            adapter.notifyDataSetChanged()
                            rl_herder.visibility = View.GONE
                            jsondata(data)
                        }

                        override fun onNot(throwable: String) {
                            updatedialog.dismiss()
                        }
                    })
                } else {
                    Httputil.deleteWtPhoto(code, this@DanjuWTKActivity, listfid, oid, object : OnUserDataInterface {
                        override fun onUserData(data: String) {
                            Log.d("onUserData","data="+data)
                            updatedialog.dismiss()
                            Myutil.MyTost("删除成功")
                            adapter.listcb.sort()
                            for (i in adapter.listcb.size-1 downTo 0){
                                wtphotobeans.removeAt(adapter.listcb[i])
                            }
                            adapter.clean()
                            adapter.notifyDataSetChanged()
                            rl_herder.visibility = View.GONE
                            getHttpPhoto()
                        }

                        override fun onNot(throwable: String) {
                            updatedialog.dismiss()
                        }
                    })
                }
            }
            tv_quxiao -> {
                rl_herder.visibility = View.GONE
                adapter.clean()
                adapter.notifyDataSetChanged()
            }
        }
    }

    public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(data==null){
            return
        }
        when (requestCode) {
            1 -> if (resultCode == Activity.RESULT_OK) {
                if (data.getBooleanExtra("SC", false)) {
                    getHttpPhoto()
                }
            }
        }
    }

    fun getHttpPhoto() {
        updatedialog = Myutil.showupdatedialog(this@DanjuWTKActivity, "获取图片信息", false)
        Httputil.getWtPhoto(code, this, oid, object : OnUserDataInterface {
            override fun onUserData(data: String) {
                updatedialog.dismiss()
                jsondata(data)
                adapter.notifyDataSetChanged()
            }

            override fun onNot(throwable: String) {
                Log.d("TAAAG", "throwable=" + Myutil.decodeUnicode(throwable))
                updatedialog.dismiss()
            }
        })
    }

    /**
     * 解析数据
     */
    private fun jsondata(data: String) {
        wtphotobeans!!.clear()
        try {
            if (code == 4) {
                val jo = JSONObject(data)
                val jo1 = JsonUtil.getjovolue(jo, "kerp")
                val js = JsonUtil.getjsvolue(jo1, "anzhuang_attachment")
                for (i in 0 until js.length()) {
                    val wtphotobean = WtPhotoBean()
                    val jo2 = js.get(i) as JSONObject
                    wtphotobean.uri = JsonUtil.getvolue(jo2, "uri")
                    wtphotobean.fid = JsonUtil.getvolue(jo2, "fid")
                    wtphotobeans!!.add(wtphotobean)
                }
            } else {
                val js = JSONArray(data)
                for (i in 0 until js.length()) {
                    val jo = js.getJSONObject(i)
                    val wtphotobean = WtPhotoBean()
                    wtphotobean.fid = JsonUtil.getvolue(jo, "fid")
                    wtphotobean.uid = JsonUtil.getvolue(jo, "uid")
                    wtphotobean.filename = JsonUtil.getvolue(jo, "filename")
                    wtphotobean.uri = JsonUtil.getvolue(jo, "uri")
                    wtphotobean.filemime = JsonUtil.getvolue(jo, "filemime")
                    wtphotobean.weight = JsonUtil.getvolue(jo, "weight")
                    wtphotobean.cnum = JsonUtil.getvolue(jo, "cnum")
                    wtphotobean.c_name = JsonUtil.getvolue(jo, "c_name")
                    wtphotobeans!!.add(wtphotobean)
                }
                for (i in wtphotobeans!!.indices) {
                    val wtPhotoBean = wtphotobeans!![i]
                    for (j in 0 until i) {
                        val wtPhotoBean1 = wtphotobeans!![j]
                        if (Integer.valueOf(wtPhotoBean.weight) < Integer.valueOf(wtPhotoBean1.weight)) {
                            if (i > j) {
                                wtphotobeans!!.add(j, wtPhotoBean)
                                wtphotobeans!!.remove(wtPhotoBean)
                            }
                        }
                    }
                }
            }
        } catch (e: JSONException) {
            e.printStackTrace()
        }

    }

    override fun onItemLongClick(parent: AdapterView<*>, view: View, position: Int, id: Long): Boolean {
        if(position < wtphotobeans.size){
            if (code == 1) {
                if (!QuanXian.iseditquality) {
                    Myutil.MyTost("你没有修改质量问题图片的权限")
                    return true
                }
            } else if (code == 2) {
                if (!QuanXian.iseditorder) {
                    Myutil.MyTost("你没有修改订单内容图片的权限")
                    return true
                }
            } else if (code == 4) {
                if (!QuanXian.isBianJiZhuanMaiDian) {
                    Myutil.MyTost("你没有修改订单内容图片的权限")
                    return true
                }
            }
            adapter.downdelete(position)
            adapter.notifyDataSetChanged()
            if(adapter.listcb.size>0) {
                rl_herder.visibility = View.VISIBLE
            }else{
                rl_herder.visibility = View.GONE
            }
        }
        return true
    }

    override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) {
        if(position < wtphotobeans.size) {
            if (adapter.listcb.size > 0) {
                adapter.downdelete(position)
                adapter.notifyDataSetChanged()
                if (adapter.listcb.size > 0) {
                    rl_herder.visibility = View.VISIBLE
                } else {
                    rl_herder.visibility = View.GONE
                }
            } else {
                val intent = Intent(this@DanjuWTKActivity, DanjianivActivity::class.java)
                intent.putExtra("url", wtphotobeans[position].uri)
                startActivity(intent)
            }
        }else if(position == wtphotobeans.size){
            if (code == 1) {
                if (!QuanXian.iseditquality) {
                    Myutil.MyTost("你没有上传质量问题图片的权限")
                    return
                }
            } else if (code == 2) {
                if (!QuanXian.iseditorder) {
                    Myutil.MyTost("你没有上传订单内容图片的权限")
                    return
                }
            } else if (code == 4) {
                if (!QuanXian.isBianJiZhuanMaiDian) {
                    Myutil.MyTost("你没有上传订单内容图片的权限")
                    return
                }
            }else if(code == 3){
                val intent = Intent(this@DanjuWTKActivity, WtPhotoActivity::class.java)
                intent.putExtra("oid", oid)
                intent.putExtra("code", 5)
                startActivity(intent)
                finish()
                return
            }
            adapter.listcb.clear()
            adapter.notifyDataSetChanged()
            rl_herder.visibility = View.GONE
            val intent = Intent(this@DanjuWTKActivity, WtPhotoActivity::class.java)
            intent.putExtra("oid", oid)
            intent.putExtra("code", code)
            startActivityForResult(intent, 1)
        }
    }
}
